<?php
/**
 * LiveCateogyModel
 *
 * PHP Version 5.4
 *
 * @author baohui  <baohui@icesmart.cn>
 */
namespace Model;

use Think\Model;

/**
 * Class ZoomCateogyrModel
 */
class LiveCateogyModel extends BaseModel
{
    /**
     * construct
     */
    public function __construct()
    {
        $this->tableName  = 'live_categories';
        $this->primaryKey = 'id';
        parent::__construct();
    }

    /**
     * 验证分类是否为空
     *
     * @param  string $field 字段名
     * @param  string $value 字段值
     * @param  string $type  (unique) 验证类型
     *
     * @return bool
     */
    public function validateField($field, $value, $type)
    {
        switch ($type) {
            case "unique":
                $category = $this->where("`{$field}` = '{$value}'")->find();
                if (!empty($category)) {
                    return false;
                }
                break;
        }

        return true;
    }

    public function structure($category)
    {

        //获取顶级分类
        foreach ($category as $v) {
            if ($v['categoryId'] == 0) {
                $cateogryTree[] = $v;
            } else {
                $childCategory [] = $v;
            }
        }
        //获取顶级分类的子分类
        foreach ($cateogryTree as $k => $v) {
            foreach ($childCategory as $k_2 => $v_2) {
                if ($v['id'] == $v_2['categoryId']) {
                    $cateogryTree[$k]['child'][$k_2] = $v_2;
                }
            }
        }

        return $cateogryTree;
    }

}